// components/my-tabbar.ts
import { IAppOption } from "../../../typings";
import { eventBus } from "../../utils/util";

const app = getApp<IAppOption>()
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tabs: app.globalData.tabs,
    currentTab: app.globalData.currentTab
  },
  pageLifetimes: {
    show() {
      this.setData({
        currentTab: app.globalData.currentTab
      })
    }
  },
  lifetimes: {
    created() {
      eventBus.on("tabSwitch", this.switchTab)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    tabChange(value: { detail: { value: string } }) {
      if (value.detail.value === this.data.currentTab) return;
      this.switchTab(value.detail.value)
    },
    switchTab(newTab: string) {
      app.globalData.currentTab = newTab
      var item = app.globalData.tabs.find(i => i.value === newTab)
      if (item !== undefined && item !== null) {
        wx.switchTab({
          url: item.url
        })
      }
      eventBus.emit("tabChange", newTab)
    }
  }
})